/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.common.utils.web.model;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import com.foreveross.bsl.common.utils.web.QueryOrder;
import com.foreveross.bsl.common.utils.web.QueryOrder.Direction;
import com.google.common.collect.Lists;

/**
 * xxx
 * 
 * @author Wangyi
 * @version v1.0
 * 
 * @date 2013-9-23
 * 
 */
public class SortInfoModel {

	private SortEntry currentSortEntry = SortEntry.NONE_SORT_ENTRY;
	private final List<SortEntry> sortEntrys = Lists.newArrayList();

	public static class SortEntry {

		public static final SortEntry NONE_SORT_ENTRY = new SortEntry("", "自动", null);

		private String name;
		private String text;
		private QueryOrder.Direction dir;
		public boolean selected;

		public SortEntry() {
		}

		public SortEntry(String name, String text, Direction dir) {
			this.name = name;
			this.text = text;
			this.dir = dir;
		}

		/**
		 * @return the name
		 */
		public String getName() {
			return name;
		}

		/**
		 * @param name
		 *            the name to set
		 */
		public void setName(String name) {
			this.name = name;
		}

		/**
		 * @return the text
		 */
		public String getText() {
			return text;
		}

		/**
		 * @param text
		 *            the text to set
		 */
		public void setText(String text) {
			this.text = text;
		}

		/**
		 * @return the dir
		 */
		public QueryOrder.Direction getDir() {
			return dir;
		}

		/**
		 * @param dir
		 *            the dir to set
		 */
		public void setDir(QueryOrder.Direction dir) {
			this.dir = dir;
		}

		public QueryOrder.Direction getNextDir() {
			if (this.equals(NONE_SORT_ENTRY)) {
				return null;
			}
			if (QueryOrder.Direction.DESC.equals(this.dir)) {
				return QueryOrder.Direction.ASC;
			} else {
				return QueryOrder.Direction.DESC;
			}
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		@Override
		public boolean equals(Object obj) {
			return EqualsBuilder.reflectionEquals(this, obj);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Object#hashCode()
		 */
		@Override
		public int hashCode() {
			return HashCodeBuilder.reflectionHashCode(this);
		}

		/**
		 * @return the selected
		 */
		public boolean isSelected() {
			return selected;
		}

		/**
		 * @param selected the selected to set
		 */
		protected void setSelected(boolean selected) {
			this.selected = selected;
		}
		
		public boolean getAsc(){
			return QueryOrder.Direction.ASC.equals(this.dir);
		}
		
		public boolean getNextAsc(){
			return QueryOrder.Direction.ASC.equals(this.getNextDir());
		}
	}

	/**
	 * @return the currentSortEntry
	 */
	public SortEntry getCurrentSortEntry() {
		return currentSortEntry;
	}

	public SortInfoModel setForCurrent(String property, QueryOrder.Direction dir){
		SortEntry se=findSortEntry(property);
		if(se!=null){
			if(this.currentSortEntry!=null && this.currentSortEntry.equals(SortEntry.NONE_SORT_ENTRY)){
				this.currentSortEntry.setSelected(false);
			}
			se.setDir(dir);
			se.setSelected(true);
			this.currentSortEntry=se;
		}
		return this;
	}
	
	public boolean getNoneSortEntry(){
		return SortEntry.NONE_SORT_ENTRY.equals(currentSortEntry);
	}
	
	public SortInfoModel changeOrder(String property, QueryOrder.Direction dir){
		SortEntry se=findSortEntry(property);
		if(se!=null){
			se.setDir(dir);
		}
		return this;
	}

	private SortEntry findSortEntry(String property){
		if(this.sortEntrys==null){
			return null;
		}
		for(SortEntry se : this.sortEntrys){
			if(se.getName().equals(property)){
				return se;
			}
		}
		return null;
	}

	/**
	 * @return the sortEntrys
	 */
	public List<SortEntry> getSortEntrys() {
		return Collections.unmodifiableList(this.sortEntrys);
	}

	/**
	 * @param sortEntrys
	 *            the sortEntrys to set
	 */
	public void setSortEntrys(List<SortEntry> sortEntrys) {
		this.sortEntrys.clear();
		this.sortEntrys.addAll(sortEntrys);
	}
	
	public SortInfoModel setSortEntry(String name, String text, boolean asc){
		SortEntry se=findSortEntry(name);
		if(se!=null){
			se.setText(text);
			se.setDir(asc ? QueryOrder.Direction.ASC : QueryOrder.Direction.DESC);
		}
		else{
			this.sortEntrys.add(new SortEntry(name, text, asc ? QueryOrder.Direction.ASC : QueryOrder.Direction.DESC));
		}
		return this;
	}
}
